package com.youhome.business.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BClientService;
import com.youhome.service.service.BShareGroupService;
import com.youhome.service.service.ListHubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/10/31.
 */
@RestController
@RequestMapping(value = "/sharegroup")
@Api(tags = "分享组")
public class ShareGroupController {

    @Autowired
    private BClientService bClientService;
    @Autowired
    private BShareGroupService bShareGroupService;
    @Autowired
    private ListHubService listHubService;

    @ApiOperation(value = "分享组列表", notes = "分享组列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "shareGroupName", value = "分享组名称", paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "counselorId", value = "顾问id", paramType = "query"),
    })
    @GetMapping(value = "/list")
    public Object list(Integer pageNumber, Integer pageSize, String shareGroupName, Long counselorId,
                       @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bShareGroupService.selectByBean(principal, pageNumber, pageSize, counselorId, shareGroupName));
    }

    @ApiOperation(value = "分享组列表", notes = "分享组列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/list/select")
    public Object listForSelect(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bShareGroupService.selectAllShareGroup(principal));
    }

    @ApiOperation(value = "分享组详情", notes = "分享组详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组UUID", required = true, paramType = "query")
    })
    @GetMapping(value = "/detail")
    public Object detail(String shareId) throws CustomException {
        return new Result(ResultCode.SUCCESS, bShareGroupService.selectByPrimaryKey(shareId));
    }

    @ApiOperation(value = "分享组房源列表", notes = "分享组房源列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组shareId", required = true, paramType = "query")
    })
    @GetMapping(value = "/house/list")
    public Object houseList(String shareId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectShareGroupHouseByShareId(shareId));
    }

    @ApiOperation(value = "删除分享组", notes = "删除分享组", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareGroupId", value = "分享组ID", required = true, paramType = "query")
    })
    @DeleteMapping(value = "/delete")
    public Object delete(String shareGroupId) throws CustomException {
        bShareGroupService.delete(shareGroupId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "分享组最近30注册用户统计", notes = "根据分享组shareId获取分享组最近30访问日志列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "query")
    })
    @GetMapping(value = "/register/last30day")
    public Object registerLast30day(String shareId) throws CustomException {
        return new Result(ResultCode.SUCCESS, bClientService.selectLast30DayDateItemByShareGroupUUID(shareId));
    }

    @ApiOperation(value = "创建分享组", notes = "创建分享组", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareGroupName", value = "分享组名称", required = true, paramType = "query")
    })
    @PostMapping(value = "/create")
    public Object create(String shareGroupName, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bShareGroupService.createShareGroup(principal, shareGroupName));
    }

    @ApiOperation(value = "分享组添加房源", notes = "分享组添加房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源Id", required = true, paramType = "path")
    })
    @PostMapping(value = "/add/{shareId}/{houseId}")
    public Object add(@PathVariable("shareId") String shareId, @PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, bShareGroupService.addHouseAppendToShareGroup(shareId, houseId));
    }

    @ApiOperation(value = "删除分享组里的房源", notes = "删除分享组里的房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源id", required = true, paramType = "path")
    })
    @DeleteMapping(value = "/delete/{shareId}/{houseId}")
    public Object delete(@PathVariable("shareId") String shareId, @PathVariable("houseId") String houseId) throws CustomException {
        bShareGroupService.deleteHouse(shareId, houseId);
        return new Result(ResultCode.SUCCESS);
    }
}
